using System;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using ICP_MS.iMass.Properties;
using LicenseManager;

namespace ICP_MS.iMass;

public class FormRegister : Form
{
	public const int InstrumentStyle = 2;

	public const string InstrumentName = "ICP-MS";

	private static int verifyResult;

	private IContainer components = null;

	private PictureBox pictureBox1;

	private GroupBox groupBox1;

	private Label lblDate;

	private Label label3;

	private Label lblProduct;

	private Label label1;

	private Button btnRegister;

	private Button btnClose;

	public FormRegister()
	{
		InitializeComponent();
		RefreshInstrumentInfos();
	}

	private void RefreshInstrumentInfos()
	{
		lblProduct.Text = "ICP-MS";
		string incode = EncryptManager.GetInstrumentCode();
		if (incode != null)
		{
			lblProduct.Text = "ICP-MS(" + incode + ")";
		}
		DateTime time = EncryptManager.GetValidDate();
		lblDate.Text = time.ToString("yyyy-MM-dd");
		if (verifyResult == -1)
		{
			groupBox1.Text = Lang.Txt("FormRegister.Register.Failure.Text");
		}
		else if (verifyResult == 1)
		{
			groupBox1.Text = Lang.Txt("FormRegister.Register.Success.Text ");
			if (CheckLicense())
			{
				btnRegister.Enabled = false;
			}
		}
		else
		{
			groupBox1.Text = Lang.Txt("FormRegister.Register.UnRegistered.Text");
		}
	}

	private void btnRegister_Click(object sender, EventArgs e)
	{
		FormLicense dlg = new FormLicense();
		if (dlg.ShowDialog() != DialogResult.OK)
		{
			return;
		}
		string incode = dlg.TxtInCode;
		incode = incode.Replace("\n", "");
		incode = incode.Trim();
		int code = 0;
		bool bIllegal = int.TryParse(incode, out code);
		string nullptr = "nullptr";
		if (incode.Length != 8 || !bIllegal)
		{
			MessageBox.Show(Lang.Txt("FormRegister.Register.IDError.Text "));
			return;
		}
		EncryptManager.CheckDBFile();
		if (!EncryptManager.InputInstrumentCode(incode))
		{
			MessageBox.Show(Lang.Txt("FormRegister.Register.FileNotFound.Text "));
			return;
		}
		string license = dlg.TxtLicense;
		if (!EncryptManager.CheckIllegal(ref license) || !EncryptManager.IsLicenseCorrect(license, 2, incode))
		{
			MessageBox.Show(Lang.Txt("FormRegister.Register.LicenseIlegal.Text "));
			return;
		}
		if (!EncryptManager.AddLicense(license, 2))
		{
			MessageBox.Show(Lang.Txt("FormRegister.Register.LicenseInvalid.Text"));
			return;
		}
		if (CheckLicense())
		{
			btnRegister.Enabled = false;
		}
		if (!EncryptManager.StoredSerialNumber(license, nullptr))
		{
			MessageBox.Show("注册失败！");
			btnRegister.Enabled = false;
		}
		else
		{
			MessageBox.Show("注册成功！");
		}
		RefreshInstrumentInfos();
	}

	private void btnClose_Click(object sender, EventArgs e)
	{
		Close();
	}

	public static bool CheckLicense()
	{
		DateTime validtime = EncryptManager.GetValidDate();
		DateTime lastTime = EncryptManager.GetLastTime();
		if (lastTime > validtime)
		{
			MessageBox.Show(Lang.Txt("FormRegister.Register.TimeInvalid.Text "));
			return false;
		}
		if (DateTime.Now > validtime)
		{
			EncryptManager.SaveLastTime(DateTime.Now);
		}
		verifyResult = EncryptManager.CheckLicense(2);
		if (verifyResult == -1)
		{
			MessageBox.Show(Lang.Txt("FormRegister.Register.TimeInvalid.Text "));
			return false;
		}
		if (verifyResult == 1)
		{
			return true;
		}
		MessageBox.Show(Lang.Txt("FormRegister.Register.UnRegistered.Text"));
		return false;
	}

	private void FormRegister_FormClosing(object sender, FormClosingEventArgs e)
	{
		if (verifyResult == 1)
		{
			base.DialogResult = DialogResult.OK;
		}
		else
		{
			base.DialogResult = DialogResult.Cancel;
		}
	}

	private void ApplyResources()
	{
		Text = Lang.Txt("FormRegister.Title.Text");
		groupBox1.Text = Lang.Txt("FormRegister.Info.Text ");
		label1.Text = Lang.Txt("FormRegister.InstrumentID.Text");
		label3.Text = Lang.Txt("FormRegister.ValidTime.Text");
		btnRegister.Text = Lang.Txt("FormRegister.Button.OK.Text ");
		btnClose.Text = Lang.Txt("FormRegister.Button.Cancel.Text");
	}

	private void FormRegister_Load(object sender, EventArgs e)
	{
		InstrumentType instrumentType = new InstrumentType();
		string path = "//iMassConfig//InstrumentType.txt";
		try
		{
			using StreamReader sr = File.OpenText(Application.StartupPath + path);
			instrumentType.InstruType = int.Parse(sr.ReadLine());
		}
		catch (Exception)
		{
			instrumentType.InstruType = 0;
		}
		if (instrumentType != null)
		{
			switch (instrumentType.InstruType)
			{
			case 0:
				pictureBox1.Image = Resources.ncsLogo;
				break;
			case 1:
				pictureBox1.Image = Resources.HexinLogo;
				break;
			case 2:
				pictureBox1.Image = Resources.MACY_Logo;
				break;
			case 3:
				pictureBox1.Image = Resources.Instrument;
				break;
			}
		}
		ApplyResources();
	}

	protected override void Dispose(bool disposing)
	{
		if (disposing && components != null)
		{
			components.Dispose();
		}
		base.Dispose(disposing);
	}

	private void InitializeComponent()
	{
		System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(ICP_MS.iMass.FormRegister));
		this.groupBox1 = new System.Windows.Forms.GroupBox();
		this.lblDate = new System.Windows.Forms.Label();
		this.label3 = new System.Windows.Forms.Label();
		this.lblProduct = new System.Windows.Forms.Label();
		this.label1 = new System.Windows.Forms.Label();
		this.btnRegister = new System.Windows.Forms.Button();
		this.btnClose = new System.Windows.Forms.Button();
		this.pictureBox1 = new System.Windows.Forms.PictureBox();
		this.groupBox1.SuspendLayout();
		((System.ComponentModel.ISupportInitialize)this.pictureBox1).BeginInit();
		base.SuspendLayout();
		this.groupBox1.Controls.Add(this.lblDate);
		this.groupBox1.Controls.Add(this.label3);
		this.groupBox1.Controls.Add(this.lblProduct);
		this.groupBox1.Controls.Add(this.label1);
		this.groupBox1.Location = new System.Drawing.Point(9, 140);
		this.groupBox1.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5);
		this.groupBox1.Name = "groupBox1";
		this.groupBox1.Padding = new System.Windows.Forms.Padding(4, 5, 4, 5);
		this.groupBox1.Size = new System.Drawing.Size(491, 122);
		this.groupBox1.TabIndex = 1;
		this.groupBox1.TabStop = false;
		this.groupBox1.Text = "信息";
		this.lblDate.Location = new System.Drawing.Point(133, 79);
		this.lblDate.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
		this.lblDate.Name = "lblDate";
		this.lblDate.Size = new System.Drawing.Size(320, 33);
		this.lblDate.TabIndex = 3;
		this.label3.AutoSize = true;
		this.label3.Location = new System.Drawing.Point(37, 79);
		this.label3.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
		this.label3.Name = "label3";
		this.label3.Size = new System.Drawing.Size(95, 24);
		this.label3.TabIndex = 2;
		this.label3.Text = "有效日期:";
		this.lblProduct.Location = new System.Drawing.Point(133, 37);
		this.lblProduct.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
		this.lblProduct.Name = "lblProduct";
		this.lblProduct.Size = new System.Drawing.Size(320, 33);
		this.lblProduct.TabIndex = 1;
		this.label1.AutoSize = true;
		this.label1.Location = new System.Drawing.Point(37, 37);
		this.label1.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
		this.label1.Name = "label1";
		this.label1.Size = new System.Drawing.Size(72, 24);
		this.label1.TabIndex = 0;
		this.label1.Text = "设备ID:";
		this.btnRegister.Location = new System.Drawing.Point(112, 281);
		this.btnRegister.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5);
		this.btnRegister.Name = "btnRegister";
		this.btnRegister.Size = new System.Drawing.Size(115, 45);
		this.btnRegister.TabIndex = 2;
		this.btnRegister.Text = "注册";
		this.btnRegister.UseVisualStyleBackColor = true;
		this.btnRegister.Click += new System.EventHandler(btnRegister_Click);
		this.btnClose.DialogResult = System.Windows.Forms.DialogResult.Cancel;
		this.btnClose.Location = new System.Drawing.Point(300, 281);
		this.btnClose.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5);
		this.btnClose.Name = "btnClose";
		this.btnClose.Size = new System.Drawing.Size(109, 45);
		this.btnClose.TabIndex = 3;
		this.btnClose.Text = "取消";
		this.btnClose.UseVisualStyleBackColor = true;
		this.pictureBox1.BackColor = System.Drawing.SystemColors.Control;
		this.pictureBox1.Image = ICP_MS.iMass.Properties.Resources.ncsLogo_2;
		this.pictureBox1.InitialImage = null;
		this.pictureBox1.Location = new System.Drawing.Point(8, 8);
		this.pictureBox1.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5);
		this.pictureBox1.Name = "pictureBox1";
		this.pictureBox1.Size = new System.Drawing.Size(492, 122);
		this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
		this.pictureBox1.TabIndex = 0;
		this.pictureBox1.TabStop = false;
		base.AutoScaleDimensions = new System.Drawing.SizeF(10f, 24f);
		base.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
		base.ClientSize = new System.Drawing.Size(509, 336);
		base.Controls.Add(this.btnClose);
		base.Controls.Add(this.btnRegister);
		base.Controls.Add(this.groupBox1);
		base.Controls.Add(this.pictureBox1);
		this.Font = new System.Drawing.Font("Arial Narrow", 12f, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, 0);
		base.Icon = (System.Drawing.Icon)resources.GetObject("$this.Icon");
		base.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5);
		base.Name = "FormRegister";
		base.ShowIcon = false;
		this.Text = "注册";
		base.FormClosing += new System.Windows.Forms.FormClosingEventHandler(FormRegister_FormClosing);
		base.Load += new System.EventHandler(FormRegister_Load);
		this.groupBox1.ResumeLayout(false);
		this.groupBox1.PerformLayout();
		((System.ComponentModel.ISupportInitialize)this.pictureBox1).EndInit();
		base.ResumeLayout(false);
	}
}
